
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
  <meta charset="utf-8">
  <title>iOS使用Bundle - 许龙武的技术博客</title>
  <meta name="author" content="许龙武">

  
  <meta name="description" content="在生成SDK的时候我们常常把资源文件打包成一个Bundle。 关于生成Bundle 方法一：可以参考iOS-生成Bundle包-引入bundle-使用bundle。
方法二：不用这么麻烦的就新建一个文件夹，把资源放进去后给文件夹加一个后缀‘.bundle’即可。 使用Bundle中的图片 1 &hellip;">
  

  <!-- http://t.co/dKP3o1e -->
  <meta name="HandheldFriendly" content="True">
  <meta name="MobileOptimized" content="320">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  
  <link rel="canonical" href="http://loongwoo.github.io/blog/2015/09/12/iosshi-yong-bundle/">
  <link href="/favicon.png" rel="icon">
  <link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
  <link href="/atom.xml" rel="alternate" title="许龙武的技术博客" type="application/atom+xml">
  <script src="/javascripts/modernizr-2.0.js"></script>
  <script src="http://cdn.staticfile.org/jquery/1.9.1/jquery.min.js"></script>
  <script>!window.jQuery && document.write(unescape('%3Cscript src="/javascripts/libs/jquery.min.js"%3E%3C/script%3E'))</script>
  <script src="/javascripts/octopress.js" type="text/javascript"></script>
  

</head>

<body   >
  <header role="banner"><hgroup>
  <h1><a href="/">许龙武的技术博客</a></h1>
  
    <h2>记录所学，快乐分享</h2>
  
</hgroup>

</header>
  <nav role="navigation"><ul class="subscription" data-subscription="rss">
  <li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
  
</ul>
  
<form action="https://www.google.com/search" method="get">
  <fieldset role="search">
    <input type="hidden" name="sitesearch" value="loongwoo.github.io">
    <input class="search" type="text" name="q" results="0" placeholder="Search"/>
  </fieldset>
</form>
  
<ul class="main-navigation">
	<li><a href="/">首页</a></li>
	<li><a href="/blog/archives">所有文章</a></li>
	<li><a href="http://weibo.com/loongwoo">我的微博</a></li>
</ul>

</nav>
  <div id="main">
    <div id="content">
      <div>
<article class="hentry" role="article">
  
  <header>
    
      <h1 class="entry-title">iOS使用Bundle</h1>
    
    
      <p class="meta">
        




<time class='entry-date' datetime='2015-09-12T18:47:28+08:00'><span class='date'><span class='date-month'>Sep</span> <span class='date-day'>12</span><span class='date-suffix'>th</span>, <span class='date-year'>2015</span></span> <span class='time'>6:47 pm</span></time>
        
      </p>
    
  </header>


<div class="entry-content"><p>在生成SDK的时候我们常常把资源文件打包成一个Bundle。</p>

<h2>关于生成Bundle</h2>

<blockquote><ul>
<li>方法一：可以参考<a href="http://blog.sina.com.cn/s/blog_7b9d64af0101jmj2.html">iOS-生成Bundle包-引入bundle-使用bundle</a>。</li>
<li>方法二：不用这么麻烦的就新建一个文件夹，把资源放进去后给文件夹加一个后缀‘.bundle’即可。</li>
</ul>
</blockquote>

<h2>使用Bundle中的图片</h2>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="bp">UIImage</span> <span class="o">*</span><span class="n">image</span> <span class="o">=</span> <span class="p">[</span><span class="bp">UIImage</span> <span class="nl">imageNamed</span><span class="p">:</span><span class="s">@&quot;MyBundle.bundle/img_collect_success&quot;</span><span class="p">];</span>
</span></code></pre></td></tr></table></div></figure>


<h2>使用Bundle中的Localizable.Strings</h2>

<p>定义以下宏：</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="cp">#define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: @&quot;MyBundle.bundle&quot;]</span>
</span><span class='line'><span class="cp">#define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH]</span>
</span><span class='line'>
</span><span class='line'><span class="c1">//获取Bundle中的字符串</span>
</span><span class='line'><span class="cp">#define MYLocalizedString(key, comment) [MYBUNDLE localizedStringForKey:(key) value:@&quot;&quot; table:nil]</span>
</span></code></pre></td></tr></table></div></figure>


<p>如果不是命名为Localizable.Strings，比如xulongwu.Strings，那么最后一个宏就得改为：</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="c1">//获取Bundle中的字符串</span>
</span><span class='line'><span class="cp">#define MYLocalizedString(key, comment) [MYBUNDLE localizedStringForKey:(key) value:@&quot;&quot; table:@&quot;xulongwu&quot;]</span>
</span></code></pre></td></tr></table></div></figure>


<p>使用的时候如下：</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="bp">NSString</span> <span class="o">*</span><span class="n">str</span> <span class="o">=</span> <span class="n">MYLocalizedString</span><span class="p">(</span><span class="s">@&quot;Settings&quot;</span><span class="p">,</span> <span class="nb">nil</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>



</div>


  <footer>
    <p class="meta">
      
  

<span class="byline author vcard">Posted by <span class="fn">许龙武</span></span>

      




<time class='entry-date' datetime='2015-09-12T18:47:28+08:00'><span class='date'><span class='date-month'>Sep</span> <span class='date-day'>12</span><span class='date-suffix'>th</span>, <span class='date-year'>2015</span></span> <span class='time'>6:47 pm</span></time>
      


      <br>原文链接：<a href="http://loongwoo.github.io/blog/2015/09/12/iosshi-yong-bundle/">http://loongwoo.github.io/blog/2015/09/12/iosshi-yong-bundle/</a>
 <!-- 添加这一行代码 -->
    </p>
    
      <div class="sharing">
  
  
  
  
  <!-- JiaThis Button BEGIN -->
<div class="jiathis_style_32x32">
  <a class="jiathis_button_qzone"></a>
  <a class="jiathis_button_tsina"></a>
  <a class="jiathis_button_tqq"></a>
  <a class="jiathis_button_weixin"></a>
  <a class="jiathis_button_renren"></a>
  <a href="http://www.jiathis.com/share?uid=2043882" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
  <a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript">var jiathis_config = {data_track_clickback:'true'};</script>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js?uid=2043882" charset="utf-8"></script>
<!-- JiaThis Button END -->

<!--UY BEGIN -->
<div id="uyan_frame"></div>
<script type="text/javascript" src="http://v2.uyan.cc/code/uyan.js?uid=2043882"></script>
<!--UY END -->

  
</div>

    
    <p class="meta">
      
        <a class="basic-alignment left" href="/blog/2015/09/12/iosduo-ge-localizable-dot-strings/" title="Previous Post: IOS多个Localizable.strings">&laquo; IOS多个Localizable.strings</a>
      
      
        <a class="basic-alignment right" href="/blog/2016/01/28/stringmanage-a-xcode-plugin-for-managing-your-strings/" title="Next Post: StringManage -- A Xcode plugin for managing your strings">StringManage -- A Xcode plugin for managing your strings &raquo;</a>
      
    </p>
  </footer>
</article>

</div>

<aside class="sidebar">
  
    <section>
  <h1>最新文章</h1>
  <ul id="recent_posts">
    
      <li class="post">
        <a href="/blog/2016/01/28/stringmanage-a-xcode-plugin-for-managing-your-strings/">StringManage -- a Xcode Plugin for Managing Your Strings</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/09/12/iosshi-yong-bundle/">iOS使用Bundle</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/09/12/iosduo-ge-localizable-dot-strings/">IOS多个Localizable.strings</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/09/11/iosxian-zhi-uitextfieldchang-du/">IOS限制UITextField长度</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/09/11/ioszi-dong-bu-ju-constraintswithvisualformat/">IOS自动布局constraintsWithVisualFormat</a>
      </li>
    
  </ul>
</section>




<section>
	<h1>关于我</h1>
    <p>程序员、创业者、IOS开发工程师
	<br/>新浪微博：<a href='http://weibo.com/loongwoo' target='_blank'>商陆_Xu</a>
	<br/>新浪博客：<a href="http://blog.sina.com.cn/lunaize" target="_blank" title="商陆的博客">商陆</a>
	<br/>GitHub：<a href='https://github.com/loongwoo' target='_blank'>Loongwoo</a>
	<br/>Git@OSC: <a href='http://git.oschina.net/redder_xu' target='_blank'>redder_xu</a>
    <br/>CSDN博客：<a href="http://blog.csdn.net/redder_xu" target="_blank" title="CSDN">霜叶红于二月花</a>
	<br/>个人微信：xulongwu77 欢迎调戏
	<br/><img width="220px" src="/images/myweixin.jpg" />
	</p>
</section>
<section>
  <h1>友情链接</h1>
  <ul>
  <li><a href="http://blog.devtang.com" target="_blank" title="唐巧的技术博客">唐巧的技术博客</a></li>
  <li><a href="http://www.leichunfeng.com" target="_blank" title="雷纯锋的技术博客">雷纯锋的技术博客</a></li>
  <li><a href="http://www.superqq.com" target="_blank" title=“刚刚在线”>刚刚在线</a></li>
  </ul>
</section>

  
</aside>


    </div>
  </div>
  <footer role="contentinfo"><p>
  Copyright &copy; 2017 - 许龙武 -
  <span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span>
  <script language="javascript" type="text/javascript" src="http://js.users.51.la/17858326.js"></script>
<noscript><a href="http://www.51.la/?17858326" target="_blank"><img alt="&#x6211;&#x8981;&#x5566;&#x514D;&#x8D39;&#x7EDF;&#x8BA1;" src="http://img.users.51.la/17858326.asp" style="border:none" /></a></noscript>

</p>

</footer>
  











</body>
</html>
